package com.woniuxy.moneyapp.service.impl;



import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.woniuxy.commons.exception.MyException;
import com.woniuxy.commons.util.BeanCopyUtil;
import com.woniuxy.moneyapp.common.exception.code.FundExceptionCode;
import com.woniuxy.moneyapp.infra.PayRecordDao;
import com.woniuxy.moneyapp.infra.dto.*;
import com.woniuxy.moneyapp.infra.model.CashRecord;
import com.woniuxy.moneyapp.infra.model.PayRecord;
import com.woniuxy.moneyapp.service.CashRecordService;
import com.woniuxy.moneyapp.service.CashoutRecordService;
import com.woniuxy.moneyapp.service.PayRecordService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import io.seata.spring.annotation.GlobalTransactional;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.Collections;
import java.util.List;


/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author seven
 * @since 2022-08-01
 */
@Service
@GlobalTransactional
public class PayRecordServiceImpl extends ServiceImpl<PayRecordDao, PayRecord> implements PayRecordService {

    @Resource
    private PayRecordService payRecordService;

    @Resource
    private CashRecordService cashRecordService;


    public boolean addInfo(RecordForm recordForm){
        //1.事务 2.同步锁（更改时 相关的三条信息状态必须一致）
        PayRecord payRecord = new PayRecord();
        payRecord.setScence(recordForm.getScence());
        payRecord.setScenceId(recordForm.getScenceId());
        payRecord.setPaymentWay(recordForm.getPayment());
        payRecord.setCashAmount(recordForm.getCashAmount());
        payRecord.setResponseTime(LocalDateTime.now());
        payRecord.setState(1);
        payRecord.setPayNum(recordForm.getSericalNum());
        payRecord.setOrderNum(recordForm.getOut_trade_no());
        if(!payRecordService
                .update(payRecord,
                        new QueryWrapper<PayRecord>()
                                .eq("pay_num",recordForm.getSericalNum())))
            throw new MyException(FundExceptionCode.DAO_ERROR.getCode(),FundExceptionCode.DAO_ERROR.getMsg());

        if(!cashRecordService.save(BeanCopyUtil.copyObject(recordForm,CashRecord.class)))
        throw new MyException(FundExceptionCode.DAO_ERROR.getCode(),FundExceptionCode.DAO_ERROR.getMsg());
        return true;
    }

    @Override
    public Page<PayRecordToShow> getPayRecord(SPayRecordForm searchh) {
        Integer pageNum = searchh.getPageNum();
        Integer pageSize = searchh.getPageSize();
        Page<PayRecordToShow> page = baseMapper.payRecord(new Page(pageNum == null ?
                1 : pageNum,
                pageSize == null ?
                        5 : pageSize),searchh);
        List<PayRecordToShow> records = page.getRecords();
        Collections.reverse(records);
        page.setRecords(records);
        return page;
    }

    @Override
    public Page<MyWalletToShow> myWalletInfo(MyWalletDto myWallet) {
        if(myWallet.getUserId()==null)
            throw new MyException(FundExceptionCode.PARAM_NULL.getCode(),FundExceptionCode.PARAM_NULL.getMsg());
        Integer pageNum = myWallet.getPageNum();
        Integer pageSize = myWallet.getPageSize();
        Page<MyWalletToShow> page = baseMapper.myWallet(new Page(pageNum == null ?
                1 : pageNum,
                pageSize == null ?
                        5 : pageSize), myWallet);
        List<MyWalletToShow> records = page.getRecords();
        Collections.reverse(records);
        page.setRecords(records);
        return page;
    }
}
